/****************************************************************************/
/***                                                                      ***/
/***   (C) 2012-2014 Dierk Ohlerich et al., all rights reserved.          ***/
/***                                                                      ***/
/***   Released under BSD 2 clause license, see LICENSE.TXT               ***/
/***                                                                      ***/
/****************************************************************************/

mtrl sDistanceFieldShader
{
    permute Multisample;
	permute Alpha;

    struct ia2vs
    {
        float3 pos : POSITION;
        float4 color : COLOR;
        float2 uv0 : TEXCOORD0;
    };
    struct vs2ps
    {
        min10float4 color : COLOR;
        min16float2 uv0 : TEXCOORD0;
    };
	
    cbuffer cbvs : vs(0) : register(0)
    {
        row_major float4x4 MS2SS;
    };
  
    cbuffer cbps : ps(0) : register(0)
    {
        float4 Para;         // sub, mul, du, dv
    };
    
    // DX9

    vs hlsl3
    {
        void main(in ia2vs i,out vs2ps o,out float4 tpos : POSITION)
        {
            tpos = mul(MS2SS,float4(i.pos,1));
            o.color = i.color;
            o.uv0 = i.uv0;
        }
    };
    ps hlsl3
    {
        SamplerState sam0 : register(s0);
  
        void main(in vs2ps i,out float4 r : COLOR)
        {
            float f = 0;
            pif(Multisample)
            {
                float4 t0 = tex2D(sam0,i.uv0+float2(Para.z , Para.w));
                float4 t1 = tex2D(sam0,i.uv0+float2(-Para.z, Para.w));
                float4 t2 = tex2D(sam0,i.uv0+float2(Para.z ,-Para.w));
                float4 t3 = tex2D(sam0,i.uv0+float2(-Para.z,-Para.w));
                float f0 = saturate((t0.r - Para.x) * Para.y);           
                float f1 = saturate((t1.r - Para.x) * Para.y);           
                float f2 = saturate((t2.r - Para.x) * Para.y);           
                float f3 = saturate((t3.r - Para.x) * Para.y);           
                f = (f0+f1+f2+f3)*0.25;
            }
            pelse
            {
                float4 tex = tex2D(sam0,i.uv0);
                f = saturate((tex.r - Para.x) * Para.y);           
            }
			pif(Alpha)
			{
				r = float4(i.color.xyz,i.color.w*f);      // no prealpha!
				if (r.w < 0.001)
					clip (-1.0);		
			}
			pelse
				r = i.color*f;      // premultiplied alpha!
        }
    };

    // DX11  
  
    vs hlsl5
    {
        void main(in ia2vs i,out vs2ps o,out float4 tpos : SV_Position)
        {
            tpos = mul(MS2SS,float4(i.pos,1));
            o.color = i.color;
            o.uv0 = i.uv0;
        }
    };
    ps hlsl5
    {
        SamplerState sam0 : register(s0);
        Texture2D tex0 : register(t0);
  
        void main(in vs2ps i,out float4 r : SV_Target)
        {
            float f = 0;
            pif(Multisample)
            {
                float4 t0 = tex0.Sample(sam0,i.uv0+float2(Para.z , Para.w));
                float4 t1 = tex0.Sample(sam0,i.uv0+float2(-Para.z, Para.w));
                float4 t2 = tex0.Sample(sam0,i.uv0+float2(Para.z ,-Para.w));
                float4 t3 = tex0.Sample(sam0,i.uv0+float2(-Para.z,-Para.w));
                float f0 = saturate((t0.r - Para.x) * Para.y);           
                float f1 = saturate((t1.r - Para.x) * Para.y);           
                float f2 = saturate((t2.r - Para.x) * Para.y);           
                float f3 = saturate((t3.r - Para.x) * Para.y);           
                f = (f0+f1+f2+f3)*0.25;
            }
            pelse
            {
                float4 tex = tex0.Sample(sam0,i.uv0);
                f = saturate((tex.r - Para.x) * Para.y);           
            }
			pif(Alpha)
			{
				r = float4(i.color.xyz,i.color.w*f);      // no prealpha!
				if (r.w < 0.001)
					clip (-1.0);		
			}
			pelse
				r = i.color*f;      // premultiplied alpha!            
        }
    };

    // glsl1
  
    vs glsl1
    {
        void main()
        {
            gl_Position = MS2SS*vec4(i_pos,1);
            o_color = i_color.zyxw;
            o_uv0 = i_uv0;
        }
    };

    ps glsl1
    {
        uniform sampler2D sam0;
        void main()
        {
            lowp float f = 0.0;
            pif(Multisample)
            {
                lowp vec4 t0 = texture2D(sam0,i_uv0+vec2(Para.z , Para.w));
                lowp vec4 t1 = texture2D(sam0,i_uv0+vec2(-Para.z, Para.w));
                lowp vec4 t2 = texture2D(sam0,i_uv0+vec2(Para.z ,-Para.w));
                lowp vec4 t3 = texture2D(sam0,i_uv0+vec2(-Para.z,-Para.w));
                lowp float f0 = ((t0.r - Para.x) * Para.y);           
                lowp float f1 = ((t1.r - Para.x) * Para.y);           
                lowp float f2 = ((t2.r - Para.x) * Para.y);           
                lowp float f3 = ((t3.r - Para.x) * Para.y);           
                f = (f0+f1+f2+f3)*0.25;
            }
            pelse
            {
                lowp vec4 tex = texture2D(sam0,i_uv0);
                f = ((tex.r - Para.x) * Para.y);           
            }
			pif(Alpha)
			{
				f = clamp(f,0.0,1.0);
				lowp vec4 c = vec4(i_color.xyz,i_color.w*f);      // no prealpha!
				if (c.w < 0.001)
					discard
				else
					gl_FragColor = c;

			}
			pelse
				gl_FragColor = i_color*f;      // premultiplied alpha!
            
        }
    };
}

